
package io.gps.datatransfer;


import io.gps.datatransfer.codec.TCPCodecFactory;
import io.gps.datatransfer.utils.SysDef;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


/**
 * DeviceController 接收端初始化
 * @author bcs
 * @version 2.0, $Date:  2020-11-26 13:04:48 $
 */
public class DeviceController {

	private Logger logger = Logger.getLogger(DeviceController.class);


	/**
	 * tcp接收端
	 */
	static TcpController tcpc;
	/**
	 * udp接收端
	 */
	static UDPController udpc;

	static long udpinitlasttime=System.currentTimeMillis();

	
	public DeviceController() throws Exception {
		init();
	}
	
	
	/**
	 * 系统数据初始化
	 */
	public void init() throws Exception {
		SysDef.init();
		TCPCodecFactory gpsDecoder = new TCPCodecFactory();
		udpc=new UDPController(gpsDecoder);
		tcpc=new TcpController(gpsDecoder);
		Timer tm = new Timer();
		tm.schedule(new RebindTimeTask(), 60*1000, 60*1000);//10*60*1000, 60*1000
	}

	class RebindTimeTask extends TimerTask{
		
		@Override
		public void run() {

			// TODO Auto-generated method stub
			logger.info(new StringBuilder("检测UDP链路：").append(new Date(udpc.getLastRecvTime())).append(" 现在是否是在允许上传时间段："+ SysDef.IS_TIME));
			if((System.currentTimeMillis()-udpc.getLastRecvTime())>60000 &&(System.currentTimeMillis()-udpinitlasttime)>60000){
				udpinitlasttime=System.currentTimeMillis();
				try {
					udpc.rebind();
				} catch (Exception e) {
					logger.error(e.getMessage(),e);
				}
			}
			
		}
		
	}

}
